package net.kong.sns.rest.client.pay;

import net.kong.sns.rest.client.method.JsonMethodInvokeBase;
import net.kong.sns.rest.client.model.PayRegOrderResponse;
import net.kong.sns.rest.client.model.PayRegOrderResponseImpl;
import net.kong.sns.rest.client.rest.HttpResponseContent;
import net.kong.sns.rest.client.InvalidResponseException;
import net.kong.sns.rest.client.KongApiMethod;

import java.util.Map;
import java.util.Set;
import java.util.HashSet;

/**
 * @author <a href="mailto:lanhuai@gmail.com">Ning Yubin</a>
 * @version $Id: PayRegOrderMethodInvoke.java,v 1.4 2009/11/04 17:17:38 ningyubin Exp $
 */
public class PayRegOrderMethodInvoke extends JsonMethodInvokeBase<PayRegOrderResponse> {
    public PayRegOrderMethodInvoke(long orderId, long amount, PayRegOrderResponse.PayType payType, String desc, String remark1, String remark2, String sessionKey) {
        super(KongApiMethod.PAY_REG_ORDER, sessionKey);
        setParameter("order_id", String.valueOf(orderId));
        setParameter("amount", String.valueOf(amount));
        setParameter("pay_type", String.valueOf(payType.getCode()));
        setParameter("desc", desc);
        setParameter("remark1", remark1);
        setParameter("remark2", remark2);
    }

    @Override
    protected PayRegOrderResponse processJsonResponse(HttpResponseContent responseData) throws InvalidResponseException {
        String response = responseData.getBody();
        Set<String> initializedFields = new HashSet<String>();
        Object o = reader.read(response);
        Map<String, Object> stringObjectMap = (Map<String, Object>) o;
        PayRegOrderResponseImpl payRegOrderResponse = new PayRegOrderResponseImpl();
        payRegOrderResponse.initialize(stringObjectMap, initializedFields);
        return payRegOrderResponse;
    }
}
